View Javadoc
1   package edu.jiangxin.apktoolbox.swing.extend;
2   
3   import edu.jiangxin.apktoolbox.utils.Constants;
4   import edu.jiangxin.apktoolbox.utils.FileUtils;
5   import edu.jiangxin.apktoolbox.utils.Utils;
6   import org.apache.commons.configuration2.Configuration;
7   import org.apache.logging.log4j.LogManager;
8   import org.apache.logging.log4j.Logger;
9   
10  import javax.swing.*;
11  import java.awt.*;
12  import java.io.File;
13  import java.util.ResourceBundle;
14  
15  /**
16   * @author jiangxin
17   * @author 2019-04-12
18   *
19   */
20  public class EasyPanel extends JPanel {
21  
22      private static final long serialVersionUID = 1L;
23      protected transient Logger logger;
24      protected transient Configuration conf;
25      protected transient ResourceBundle bundle;
26  
27      protected boolean isInited = false;
28      
29      public EasyPanel() throws HeadlessException {
30          super();
31          logger = LogManager.getLogger(this.getClass().getSimpleName());
32          conf = Utils.getConfiguration();
33          bundle = ResourceBundle.getBundle("apktoolbox");
34          logger.info("Panel start: " + this.getClass().getSimpleName());
35      }
36  
37      public boolean isNeedPreChangeMenu() {
38          return false;
39      }
40  
41      public void init() {
42          if (!isInited) {
43              initUI();
44              isInited = true;
45          }
46      }
47  
48      public void initUI() {
49          // do nothing
50      }
51  
52      public void afterPainted() {
53          // do nothing
54      }
55  
56      protected EasyFrame getFrame() {
57          //Java/Swing: Obtain Window/JFrame from inside a JPanel:
58          // https://stackoverflow.com/questions/9650874/java-swing-obtain-window-jframe-from-inside-a-jpanel
59          Window window = SwingUtilities.getWindowAncestor(this);
60          if (window instanceof EasyFrame) {
61              return (EasyFrame) window;
62          }
63          return null;
64      }
65  
66      protected String checkAndGetFileContent(JTextField textField, String key, String msg) {
67          File file = new File(textField.getText());
68          if (!file.exists() || !file.isFile()) {
69              logger.error(msg);
70              Toolkit.getDefaultToolkit().beep();
71              JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
72                      JOptionPane.ERROR_MESSAGE);
73              textField.requestFocus();
74              return null;
75          }
76          String path = FileUtils.getCanonicalPathQuiet(file);
77          if (path != null) {
78              conf.setProperty(key, path);
79          }
80          return path;
81      }
82  
83      protected String checkAndGetNewFileContent(JTextField textField, String key, String msg) {
84          File file = new File(textField.getText());
85          File parentFile = file.getParentFile();
86          if (!parentFile.exists() || !parentFile.isDirectory()) {
87              logger.error(msg);
88              Toolkit.getDefaultToolkit().beep();
89              JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
90                      JOptionPane.ERROR_MESSAGE);
91              textField.requestFocus();
92              return null;
93          }
94          String path = FileUtils.getCanonicalPathQuiet(file);
95          if (path != null) {
96              conf.setProperty(key, path);
97          }
98          return path;
99      }
100 
101     protected String checkAndGetDirContent(JTextField textField, String key, String msg) {
102         File file = new File(textField.getText());
103         if (!file.exists() || !file.isDirectory()) {
104             logger.error(msg);
105             Toolkit.getDefaultToolkit().beep();
106             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
107                     JOptionPane.ERROR_MESSAGE);
108             textField.requestFocus();
109             return null;
110         }
111         String path = FileUtils.getCanonicalPathQuiet(file);
112         if (path != null) {
113             conf.setProperty(key, path);
114         }
115         return path;
116     }
117 
118     protected String checkAndGetStringContent(JTextField textField, String key, String msg) {
119         String content = textField.getText();
120         if (content == null || content.isEmpty()) {
121             logger.error(msg);
122             Toolkit.getDefaultToolkit().beep();
123             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
124                     JOptionPane.ERROR_MESSAGE);
125             textField.requestFocus();
126             return null;
127         }
128         conf.setProperty(key, content);
129         return content;
130     }
131 }